﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{
    internal class BannerProvider
    {
        private ArbolandiaDBDataContext _db;
        private ImageProvider _imgprov;
        private BannerType _bt;

        public BannerProvider(BannerType bt)
        {
            _db = new ArbolandiaDBDataContext();
            _imgprov = new ImageProvider();
            _bt = bt;
        }

        internal void InsertBanner(Guid guid, Picture picture)
        {

            //Guardamos la imagen y la damos de alta en arb_picture
            picture.GuidId = guid;
            _imgprov.InsertUpdateImage(picture);

            //actualizamos el guid en la tabla de subcategorias
            UpdatePictureGuidBanner(guid);

        }
        internal void UpdatePictureGuidBanner(Guid guid)
        {
            var banner = new Banners()
            {
                Description = "",
                PageId = 0,
                PictureGuidId = guid,
                Position = (short)_bt
            };
            _db.Banners.InsertOnSubmit(banner);
            _db.SubmitChanges();
        }
        internal void DeleteBanner(int id)
        {
            var query2 = from s in _db.Banners
                         join p in _db.Picture on s.PictureGuidId equals p.GuidId
                         where s.Id == id
                         select p;
            if (query2.Count() > 0)
            {
                _db.Picture.DeleteOnSubmit(query2.First());
                _db.SubmitChanges();
            }

            var query = _db.Banners.Where(w => w.Id == id);
            if (query.Count() > 0) { _db.Banners.DeleteOnSubmit(query.First()); _db.SubmitChanges(); }
        }
        internal List<object> GetBanners()
        {
            var res = new List<object>();
            var query = from b in _db.spGetPictureBanners((int)_bt)
                        select new
                        {
                            Id = b.Id,
                            LinkUrl = b.LinkUrl,
                            url = b.url
                        };
            foreach (var item in query)
            {
                res.Add(new
                {
                    Id = item.Id,
                    LinkUrl = item.LinkUrl,
                    url = item.url
                });
            }
            return res;

        }

    }
}
